<template>
  <div class="divBox">
    <div class="selCard">
      <el-form :model="tableFrom" ref="searchForm" size="small" inline label-width="85px">
        <el-form-item label="创建时间：">
          <el-date-picker
            v-model="timeVal"
            value-format="yyyy/MM/dd"
            format="yyyy/MM/dd"
            type="daterange"
            placement="bottom-end"
            placeholder="自定义时间"
            style="width: 280px;"
            :picker-options="pickerOptions"
            clearable
            @change="onchangeTime"
          />
        </el-form-item>
        <el-form-item label="分账时间：">
          <el-date-picker
            v-model="timeVal2"
            value-format="yyyy/MM/dd"
            format="yyyy/MM/dd"
            size="small"
            type="daterange"
            placement="bottom-end"
            placeholder="自定义时间"
            style="width: 280px;"
            :picker-options="pickerOptions"
            clearable
            @change="onchangeTime2"
          />
        </el-form-item>
        <el-form-item label="状态：" prop="status">
          <el-select
            v-model="tableFrom.status"
            placeholder="请选择"
            class="selWidth"
            clearable
            @change="getList(1)"
          >
            <el-option v-for="item in applyStatus" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="账单类型：" prop="type">
          <el-select
            v-model="tableFrom.type"
            placeholder="请选择"
            class="selWidth"
            clearable
            @change="getList(1)"
          >
            <el-option label="订单支付" value="order" />
            <el-option label="尾款支付" value="presell" />
          </el-select>
        </el-form-item>
        <el-form-item label="商户名称：" prop="mer_id">
          <el-select
            v-model="tableFrom.mer_id"
            clearable
            filterable
            placeholder="请选择"
            class="selWidth"
            @change="getList(1)"
          >
            <el-option
              v-for="item in merSelect"
              :key="item.mer_id"
              :label="item.mer_name"
              :value="item.mer_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="getList(1)">搜索</el-button>
          <el-button size="small" @click="searchReset()">重置</el-button> 
        </el-form-item>
      </el-form> 
    </div>
    <el-card class="mt14">
      <div class="mb20">
        <el-button size="small" type="primary" @click="exports">导出列表</el-button>
      </div>
      <el-table v-loading="listLoading" :data="tableData.data" size="small">
        <el-table-column prop="profitsharing_id" label="分账ID" min-width="60" />
        <el-table-column prop="order.order_sn" label="订单编号" min-width="100" />
        <el-table-column prop="merchant.mer_name" label="商户名称" min-width="100" />
        <el-table-column label="订单金额" min-width="100">
          <template slot-scope="scope">
            <div>分账金额： {{ scope.row.profitsharing_price }}</div>
            <div v-if="scope.row.profitsharing_refund > 0" style="color:#82e493;">退款金额： {{ scope.row.profitsharing_refund }}</div>
            <div>分账给商户金额： {{ scope.row.profitsharing_mer_price }}</div>
          </template>
        </el-table-column>
        <el-table-column label=" 账单类型" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.type == 'order' ? '订单支付' : '尾款支付' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" min-width="120">
          <template slot-scope="scope">
            <div v-if="scope.row.status == 0">未分账</div>
            <div v-else-if="scope.row.status == 1">已分账<br>分账时间： {{ scope.row.profitsharing_time }}</div>
            <div v-else-if="scope.row.status == -1">已退款</div>
            <div v-else-if="scope.row.status == -2">分账失败<br> <span style="color: red; font-size: 12px;"> 失败原因： {{ scope.row.error_msg }}</span></div>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" min-width="120" />
        <el-table-column label="操作" min-width="100" fixed="right">
          <template slot-scope="scope">
            <el-button
            v-if="scope.row.status == -2"
              type="text"
              size="small"
              class="mr10"
              @click="splitAccount(scope.row.profitsharing_id)"
            >立即分账</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import { applymentLstApi, splitAccountApi, ledgerManageExportApi } from '@/api/merchant'
import { merSelectApi } from "@/api/product";
import createWorkBook from '@/utils/newToExcel.js';
import timeOptions from '@/utils/timeOptions';
export default {
  components: { },
  data() {
    return {
      pickerOptions: timeOptions,
      tableData: {
        data: [],
        total: 0
      },
      merSelect: [],
      listLoading: true,
      tableFrom: {
        type: '',
        mer_id: '',
        keyword: '',
        status: '',
        date: '',
        profit_date: '',
        page: 1,
        limit: 20
      },
      timeVal: [],
      timeVal2: [],
      fromList: {
        title: '选择时间',
        custom: true,
        fromTxt: [
          { text: '全部', val: '' },
          { text: '今天', val: 'today' },
          { text: '昨天', val: 'yesterday' },
          { text: '最近7天', val: 'lately7' },
          { text: '最近30天', val: 'lately30' },
          { text: '本月', val: 'month' },
          { text: '本年', val: 'year' }
        ]
      },
      selectionList: [],
      ids: '',
      LogLoading: false,
      applyStatus: [
        { value: 0, label: '待分账' },
        { value: 1, label: '已分账' },
        { value: -1, label: '已退款' },
        { value: -2, label: '分账失败' }
      ],
      orderDatalist: null
    }
  },
  mounted() {
    this.getList('')
    this.getMerSelect();
  },
  methods: {
    /**重置 */
    searchReset(){
      this.timeVal = []
      this.timeVal2 = []
      this.tableFrom.date = ""
      this.tableFrom.profit_date = ""
      this.$refs.searchForm.resetFields()
      this.getList(1)
    },
    // 商户列表；
    getMerSelect() {
      merSelectApi()
        .then(res => {
          this.merSelect = res.data;
        })
        .catch(res => {
          this.$message.error(res.message);
        });
    },
    // 立即分账
    splitAccount(id){
        this.$confirm('是否确认分账?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            splitAccountApi(id).then(res => {
                this.$message.success(res.message);
                this.getList('')
            }).catch(res => {
                this.$message.error(res.message);
                this.getList('')
            });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e
      this.tableFrom.date = e ? this.timeVal.join('-') : ''
      this.getList(1)
    },
    onchangeTime2(e) {
      this.timeVal2 = e
      this.tableFrom.profit_date = e ? this.timeVal2.join('-') : ''
      this.getList(1)
    },
    async exports(value) {
      let excelData = JSON.parse(JSON.stringify(this.tableFrom)), data = []
      excelData.page = 1
      let pageCount = 1
      let lebData = {};
      for (let i = 0; i < pageCount; i++) {
        lebData = await this.downData(excelData)
        pageCount = Math.ceil(lebData.count/excelData.limit)
        if (lebData.export.length) {
          data = data.concat(lebData.export)
          excelData.page++
        }  
      }
      createWorkBook(lebData.header, lebData.title, data, lebData.foot,lebData.filename);
      return
    },
    /**转账记录 */
    downData(excelData) {
      return new Promise((resolve, reject) => {
        ledgerManageExportApi(excelData).then((res) => {
          return resolve(res.data)
        })
      })
    },
    // 列表
    getList(num) {
      this.listLoading = true
      this.tableFrom.page = num || this.tableFrom.page
      applymentLstApi(this.tableFrom)
        .then(res => {
          this.tableData.data = res.data.list
          this.tableData.total = res.data.count
          this.listLoading = false
        })
        .catch(res => {
          this.$message.error(res.message)
          this.listLoading = false
        })
    },
    pageChange(page) {
      this.tableFrom.page = page
      this.getList('')
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList('')
    }
  }
}
</script>

<style lang="scss" scoped>
table .el-image{
  display: inline-block;
}
.el-dropdown-link {
  cursor: pointer;
  color: var(--prev-color-primary);
  font-size: 12px;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.tabBox_tit {
  width: 60%;
  font-size: 12px !important;
  margin: 0 2px 0 10px;
  letter-spacing: 1px;
  padding: 5px 0;
  box-sizing: border-box;
}
.mt20 {
  margin-top: 20px;
}
.demo-image__preview{
  position: relative;
}
.maxw180{
  display: inline-block;
  max-width: 180px;
}

</style>
